"use server";
import Title from "antd/es/typography/Title";
import { message, Card } from "antd";
import { listQuestionVoByPageUsingPost } from "@/api/questionController";
import QuestionTable from "@/components/QuestionTable";
import "./index.css";


/**
 * 题目列表页面
 * @constructor
 */
export default async function QuestionsPage({ searchParams }) {
    // 获取 url 的查询参数
    const { q: searchText, tags: selectedTags } = searchParams;
    // 题目列表和总数
    let questionList = [];
    let total = 0;

    try {
        const res = await listQuestionVoByPageUsingPost({
            title: searchText,
            tags: selectedTags ? (Array.isArray(selectedTags) ? selectedTags : [selectedTags]) : undefined,
            pageSize: 10,
            sortField: "createTime",
            sortOrder: "descend",
        });
        questionList = res.data.records ?? [];
        total = res.data.total ?? 0;
    } catch (e) {
        message.error("获取题目列表失败，" + e.message);
    }

    return (
        <div id="questionsPage" className="questions-page-container">
            <div className="page-header">
                <Title level={2} className="page-title">题目大全</Title>
                <p className="page-subtitle">探索各种编程题目，提升你的技能</p>
            </div>

            <Card className="questions-card">
                <QuestionTable
                    defaultQuestionList={questionList}
                    defaultTotal={total}
                    defaultSearchParams={{
                        title: searchText,
                        tags: selectedTags
                    }}
                />
            </Card>
        </div>
    );
}